<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
global $charts, $show_ads, $path, $language, $highlight5, $dal, $premium, $showPromotionTab, $promotionPoints;


$url = parse_url( $_SERVER[ 'REQUEST_URI' ] );
$path = explode( '/', $url[ 'path' ] );
if( count( $path > 1 ) ) {
	$path = $path[ count( $path ) - 2];
	//print( "Path: " . $path . "<br />\r\n" );
}
else {
	$path = "";
}
$charts = false;
if( !$show_ads && ( $path == 'overview' || $path == 'villages' || $path == 'armies' || $path == 'diplomacy' || $path == 'operations' || $path == 'trade' ) ) {
	$charts = true;
}
$query = "SELECT count(*) " .
		"FROM news " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND seen = 0" ;
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$highlight = "";
if( $row[ 'count(*)' ] > 0 ) {
	$highlight = " highlight";	
}
$query = "SELECT count(*) " .
		"FROM message_ruler JOIN messages ON message_ruler.message_id = messages.message_id " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND seen = 0 AND hide = 0";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$highlight2 = "";
if( $row[ 'count(*)' ] > 0 ) {
	$highlight2 = " highlight";	
}

$query = "SELECT faction_id " .
		"FROM faction_member " .
		"WHERE invited = 0 AND requested = 0 AND ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'faction_id' ] ) ) {
	$faction_id = $row[ 'faction_id' ];
}
else {
	$faction_id = -1;
}
global $shoutbox;
$shoutbox = new Shoutbox();
if( $shoutbox->get_unread_chat_message_count( $_SESSION[ 'ruler_id' ], $faction_id ) > 0 ) {
	$highlight2 = " highlight";
}

$query = "SELECT `updated` " .
		"FROM ruler_task " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND `completed` = 0";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( isset( $row[ 'updated' ] ) && $row[ 'updated' ] == 1 ) {
	$highlight5 = " highlight";
}
else {
	$highlight5 = "";
}

$url = parse_url( $_SERVER[ 'REQUEST_URI' ] );
$path = explode( '/', $url[ 'path' ] );
if( count( $path > 1 ) ) {
	$path = $path[ count( $path ) - 2];
	//print( "Path: " . $path . "<br />\r\n" );
}
else {
	$path = "";
}

$query = "SELECT date, turn_update, turn, max_turn, update_time, start_time, game_running  " .
		"FROM game";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$turn_update = $row[ 'turn_update' ];
$update_time = $row[ 'update_time' ];
$turn = $row[ 'turn' ];
$max_turn = $row[ 'max_turn' ];
$date = $row[ 'date' ];
$startTime = $row[ 'start_time' ];
$gameRunning = $row[ 'game_running' ];
if( isset( $_SESSION[ 'admin' ] ) && $_SESSION[ 'admin' ] == 1 ) {
	if( $path == 'admin' ) {
		$tmp_admin = "<a class=\"tab-link selected\" href=\"../admin/admin.php\" title=\"" . translate( 'Game administration tools' ) . "\">" . translate( 'Admin' ) . "</a>";
	}
	$tmp_admin = "<a class=\"tab-link\" href=\"../admin/admin.php\">" . translate( 'Admin' ) . "</a>";
	$tmp_admin = "<li class=\"tab-item\">$tmp_admin</li>";
}
else {
	$tmp_admin = "";
}

if( can_publish_history() ) {
	$query = "SELECT count(*) " .
			"FROM history_seen " .
			"JOIN history ON history.history_id = history_seen.history_id " .
			"WHERE seen = 0 AND hide = 0 AND ( ( visible = 1 AND turn <= $turn ) OR visible = 0 ) AND history_seen.ruler_id = " . $_SESSION[ 'ruler_id' ];
}
else {
	$query = "SELECT count(*) " .
			"FROM history_seen " .
			"JOIN history ON history.history_id = history_seen.history_id " .
			"WHERE seen = 0 AND visible = 1 AND hide = 0 " .
			"AND history_seen.ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND turn <= $turn";
} 

//print( "Query: $query <br />" );
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( $row[ 'count(*)' ] > 0 ) {
	$highlight4 = " highlight";
}
else {
	$highlight4 = "";
}
?>
			<div id="header">
<?php
if( $turn_update == 1 ) { 
	if( $update_time != -1 ) {
		$duration = round( $update_time * 0.001, 1 );
		if( $duration > 60 ) {
			$minutes = floor( $duration / 60 );
			$seconds = round( $duration % 60 );
			$duration = $minutes . ' ' . translate( 'minutes' ) . ' ' . translate( 'and' ) . ' ' . $seconds . ' ' . translate( 'seconds' );
		}
		else {
			$duration = $duration . ' ' . translate( 'seconds' );
		}
		$duration = ' (' . translate( 'estimated duration' ) . ': ' . $duration . ')';
	}
	else {
		$duration = '';
	}
	print( "<div id=\"update-text-container\">" .
			"<small class=\"update-text\">" . translate( 'Updating' ) . $duration . '...' . "</small>" .
			"</div><br />\r\n" );
}
elseif( $premium < 1 && rand( 0, 9 ) < 1 ) {
	$advantages = Array(
							translate( 'Each turn your officers receive 5 experience points instead of just 2' ), 
							translate( 'You can set a custom army base name in account settings' ), 
							translate( 'You are not shown any advertisement, which means the site will load faster' ),
							translate( 'You have a special list showing a lot of information among which a list of your 5 last active buddies' ),
							translate( 'You can view charts about your progress, armies, villages and more' ),
							translate( 'You can queue up to 9 buildings, upgrades and expeditions instead of only 3' ),
							translate( 'You can create 9 trade offers at once instead of only 3' ),
							translate( 'Auto trade offers created every 3 game days 9 instead of only 3' ),
							translate( 'You can store up to 20 locations instead of only 3' ),
							translate( 'You get offered private trades by independent traders' ),
							translate( 'You can set your workers to be automatically assigned per village' ),
							translate( 'Sort trades by requested and/or offered resource' )
	);
?>
						<div id="premium-advantage-box">
							<a href="../global/shop.php?show=advantages"><?php transprint( 'Premium advantage' ); ?>: <?php print( $advantages[rand( 0, count( $advantages ) - 1 )] ); ?></a>
						</div>
						<br />
						<br />
<?php
}

$time = time();

if( $gameRunning == 0 && $startTime > $time ) {
	$untilStart = $startTime - $time;
	$days = floor( $untilStart / 86400 ); 
	$hours = floor( ( $untilStart % 86400 ) / 3600 );
	
	if( $days == 0 && $hours == 0 ) {
		$message = translate( 'Less then an hour until the game starts, get ready!' );
	}
	elseif( $days == 0 ) {
		if( $hours == 1 ) {
			$message = translate( 'Just over an hour until the game starts.' );
		}
		else {
			$message = $hours . ' ' . translate( 'hours left until the game starts' ) . '.';
		}
	}
	else {
		if( $days == 1 ) {
			$message = translate( 'There is 1 day' );
		}
		else {
			$message = translate( 'There are' ) . ' ' . $days . ' ' . translate( 'days' );
		}
		
		if( $hours == 0 ) {
			$message .= ' ' . translate( 'left until the game starts' ) . '.';
		}
		elseif( $hours == 1 ) {
			$message .= ' ' . translate( 'and' ) . ' 1 ' . translate( 'hour' ) . ' ' . translate( 'left until the game starts' ) . '.';
		}
		else {
			$message .= ' ' . translate( 'and' ) . ' ' . $hours . ' ' . translate( 'hours' ) . ' ' . translate( 'left until the game starts' ) . '.';
		}
	}
?>	
						<div id="start-time-box">
							<?php transprint( 'No turn updates at the moment' ); ?>: <?php print( $message ); ?>
						</div>
						<br />
						<br />
<?php
}
?>
				<ul class="tab-menu">
<?php		

$selected = " class=\"tab-link$highlight5\"";
if( $path == 'overview' ) {
	$selected = " class=\"tab-link selected$highlight5\"";
}
print( "<li class=\"tab-item\">\r\n" );
print( "<a$selected href=\"../overview/overview.php\" title=\"" . translate( 'Information about your empire' ) . "\">" . translate( 'Overview' ) . "</a>\r\n" );
?>
					</li>
<?php

$selected = " class=\"tab-link\"";
if( $path == 'villages' ) {
	$selected = " class=\"tab-link selected\"";
}

$statement = $dal->prepare( 'SELECT name ' .
		'FROM village ' .
		'WHERE ruler_id = :ruler_id ' .
		'LIMIT 2' );
$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
$names = $dal->getArrayOfObjects( $statement );
if( count( $names ) == 1 ) {
	$villageUrl = '../villages/village.php?name=' . $names[0]->name;
}
else {
	$villageUrl = '../villages/villages.php';
}
print( "<li class=\"tab-item\">\r\n" );
print( "<a$selected href=\"$villageUrl\" title=\"" . translate( 'Construct new buildings and inspect on how well your villages are doing' ) . "\">" . translate( 'Villages' ) . "</a>" );
?>
					</li>
<?php

$selected = " class=\"tab-link\"";
if( $path == 'armies' ) {
	$selected = " class=\"tab-link selected\"";
}
print( "<li class=\"tab-item\">\r\n" );
print( "<a$selected href=\"../armies/armies.php\" title=\"" . translate( 'Train soldiers and give orders to your armies' ) . "\">" . translate( 'Armies' ) . "</a>" );
?>
					</li>
<?php

$selected = " class=\"tab-link$highlight2\"";
if( $path == 'diplomacy' ) {
	$selected = " class=\"tab-link selected$highlight2\"";
}
print( "<li class=\"tab-item\">\r\n" );
print( "<a$selected $highlight2 href=\"../diplomacy/diplomacy.php\" title=\"" . translate( 'Contact other rulers and form or join a faction' ) . "\">" . translate( 'Diplomacy' ) . "</a>" );
?>
					</li>
<?php

$selected = " class=\"tab-link$highlight3\"";
if( $path == 'operations' ) {
	$selected = " class=\"tab-link selected$highlight3\"";
}
print( "<li class=\"tab-item\">\r\n" );
print( "<a$selected $highlight3 href=\"../operations/map.php\" title=\"" . translate( 'Keep track of armies and villages' ) . "\">" . translate( 'Operations' ) . "</a>" );
?>
					</li>
<?php

$selected = " class=\"tab-link\"";
if( $path == 'trade' ) {
	$selected = " class=\"tab-link selected\"";
}
print( "<li class=\"tab-item\">\r\n" );
print( "<a$selected href=\"../trade/trade.php\" title=\"" . translate( 'Trade resources with other rulers or independent traders' ) . "\">" . translate( 'Trade' ) . "</a>" );
?>
					</li>
<?php

$selected = " class=\"tab-link$highlight\"";
if( $path == 'news' ) {
	$selected = " class=\"tab-link selected$highlight2\"";
}
print( "<li class=\"tab-item\">\r\n" );
print( "<a$selected $highlight href=\"../news/news.php\" title=\"" . translate( 'News about your empire' ) . "\">" . translate( 'News' ) . "</a>" );
?>
					</li>
<?php

$selected = " class=\"tab-link\"";
if( $path == 'upgrade' ) {
	$selected = " class=\"tab-link selected\"";
}
print( "<li class=\"tab-item\">\r\n" );
print( "<a$selected href=\"../upgrade/upgrade.php\" title=\"" . translate( 'Upgrades help you expand your empire' ) . "\">" . translate( 'Upgrades' ) . "</a>" );

$selected = " class=\"tab-link\"";
if( $path == 'aosmanual' ) {
	$selected = " class=\"tab-link selected\"";
}
print( "<li class=\"tab-item\">\r\n" );
print( "<a$selected href=\"../aosmanual/manual.php\" title=\"" . translate( 'Look here for information about the game' ) . "\">" . translate( 'Manual' ) . "</a>" );
?>
					</li>
<?php

if( $showPromotionTab ) {
	$selected = " class=\"tab-link\"";
	if( $path == 'promotion' ) {
		$selected = " class=\"tab-link selected\"";
	}
?>
					<li class="tab-item">
						<a<?php print( $selected ); ?> href="../promotion/promotion.php" title="<?php transprint( 'Check out how you can promote Ages of Strife and what rewards you will receive' ); ?>"><?php transprint( 'Promotion' ); ?></a>
					</li>
<?php
}
print( $tmp_admin );
print( "<li class=\"tab-item\">\r\n" );
print( "<a class=\"simple-tab-link\" href=\"../index.php?language=$language\" title=\"" . translate( 'Log out your user account' ) . "\">" . translate( 'Logout' ) . "</a></li>" );
?>
					</li>
<?php
$selected = " class=\"tab-link turns$highlight4\"";
if( $path == 'history' ) {
	$selected = " class=\"tab-link selected turns$highlight4\"";
}

print( "<li class=\"date tab-item\">\r\n" );
print( "\t\t<a href=\"../history/history.php\"$selected title=\"" . translate( 'Day' ) . " " . number_format( $turn ) . " " . translate( 'out of' ) . " " . number_format( $max_turn ) . " " . translate( 'days in this round' ) . "\">$date</a>\r\n" );
?>
					</li>
				</ul>
			</div> <!-- close header -->
<?php
if( $charts ) {
?>
			<div class="hidden" id="charts-container">
				<div class="hd"><?php transprint( 'Charts' ); ?></div>
				<div class="bd">
				</div>
				<a onclick="chartsPanel.hide();" href="javascript:void( null )">
					<span class="container-close"> </span>
				</a>			
			</div>
			<div class="hidden" id="current-tab"><?php print( $path ); ?></div>
<?php
}
?>
	
